/*
 *输入：二叉树前序序列、中序序列
 *输出：重建的二叉树
 * */

#include <iostream>
#include <vector>

using namespace std;

struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
	TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
		if(pre.size() == 0)
			return NULL;
		vector<int> pre_one;
		vector<int> pre_two;
		vector<int> vin_one;
		vector<int> vin_two;
												        
		int head = pre.front();
		vector<int> *flag = &vin_one;
		for(vector<int>::iterator i = vin.begin();i < vin.end();++i){

			if(*i == head){
				flag = &vin_two;
				continue;
			}
			else flag->push_back(*i);
		}
		for(int j = 1;j <= vin_one.size();++j){
			pre_one.push_back(pre[j]);
		}
		for(int j = vin_one.size() + 1;j < pre.size();++j){
			pre_two.push_back(pre[j]);
		}
		TreeNode *ro = new TreeNode(head);
		ro->left = reConstructBinaryTree(pre_one,vin_one);
		ro->right = reConstructBinaryTree(pre_two,vin_two);
																												        
		return ro;
	}
};

//递归后序遍历二叉树
void travePost(TreeNode *root){
	if(!root){
		return;
	}
	travePost(root->left);
	travePost(root->right);
	cout<<root->val<<" ";

}


int main(){
	int a;
	vector<int> pre;
	vector<int> vin;
	for(int i = 0;i < 8;++i){
		cin>>a;
		pre.push_back(a);
	}
	for(int i = 0;i < 8;++i){
		cin>>a;
		vin.push_back(a);
	}

	Solution yes;
	TreeNode *root = yes.reConstructBinaryTree(pre,vin);
	travePost(root);
	return 0;
}
